package com.miracle9.game.thread;

import com.miracle9.game.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

@Service("dealCheatThread")
public class DealCheatThread extends Thread {

    private static final Logger logger = Logger.getLogger(DealCheatThread.class);
    @Autowired
    private UserService userService;
    private static BlockingQueue<Integer> T = new LinkedBlockingQueue();

    @PostConstruct
    public void dealCheatThreadStart() {
        start();
    }

    public static void f(int userId) {
        try {
            logger.info("addCheatUser userId:" + userId);
            T.add(Integer.valueOf(userId));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run() {
        try {
            for (; ; ) {
                logger.info("DealCheatThread run");
                Integer userId;
                if ((userId = (Integer) T.take()) != null) {
                    logger.info("twice freeze user userId:" + userId);
                    Random random = new Random();
                    int time = random.nextInt(20000);
                    Thread.sleep(time + 3000);
                    this.userService.updateStatus(userId.intValue(), 3);
                    UserService.quitToLogin(userId.intValue(), 2);
                    logger.info("twice freeze user userId:" + userId + " success");
                }
            }
        } catch (Exception e) {
            logger.error("Run DealCheatThread error:", e);
        }
    }

}
